var AV = require('leanengine');
var api = require('./api');

/**
 * 一个简单的云代码方法
 */
AV.Cloud.define('hello', function(request, response) {

  var urlString = "http://www.sf-express.com/sf-service-web/service/bills/590338957293/routes?app=bill&lang=sc&region=cn&translate=";
  AV.Cloud.httpRequest({
    method: 'GET',
      url: urlString, 
      success: function(httpResponse) {
      var jsonResult = JSON.parse(httpResponse.text);
      response.success(jsonResult);
    }, error: function(httpResponse) {
        response.success('失败22');
    }});

});

// API: 查询快递信息
AV.Cloud.define("fetchKuaiDiInfo", function(request, response) {
  
    api.fetchKuaiDiInfo(request.params.expressCompany,request.params.expressNumber,function (data) {
      // console.log(data);
      // console.log("查询快递结束 "+request.params.expressCompany+" "+request.params.expressNumber);
      response.success(data);
    });
});

AV.Cloud.define('fetchKuaiDiInfoInBackground', function(request, response) {
  
    api.fetchKuaiDiInfoInBackground();
    response.success();
});

// API: 添加到查询的快递队列中
AV.Cloud.define("addExpressForTracking", function(request, response) {
  
    api.addExpressForTracking(request.params.expressCompany,request.params.expressNumber,function (data) {
        response.success(data);
    });
});

// API: 推送
AV.Cloud.define('pushExpressUpdateInfoToUser', function(request, response) {
  
    api.pushExpressUpdateInfoToUser(request.params.messageTitle, request.params.messageBody, request.params.channel, function (data) {
      console.log(data);
      response.success(data);
    });
});

module.exports = AV.Cloud;
